using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace MedicalSystem.SmartReport.Mobile
{
   public class SmartReportManager
    {
       private static Dictionary<string, ComboBoxElement> _comboxElementDictionary = new Dictionary<string, ComboBoxElement>();
       private static XmlDocument _xmlDocument = new XmlDocument();
       private static string _fileName;
       public static Dictionary<string, ComboBoxElement> GetComboxUIElement(string fileName)
       {
           _comboxElementDictionary.Clear();

           _fileName = fileName;

          _xmlDocument.Load(fileName);

          XmlNodeList xnl = _xmlDocument.SelectNodes("/Object/Object");
           foreach (XmlNode xn in xnl)
           {
               ReadComboBox(xn);
           }
           return _comboxElementDictionary;
       }

       public static void WriteToConfigure(Dictionary<string, ComboBoxElement> comboBoxUIElement)
       {
           if (string.IsNullOrEmpty(_fileName))
               throw new ArgumentNullException("fileName");

           _comboxElementDictionary = comboBoxUIElement;
           XmlNodeList xnl = _xmlDocument.SelectNodes("/Object/Object");
           foreach (XmlNode xn in xnl)
           {
               WriteComboBoxItems(xn);
           }

           _xmlDocument.Save(_fileName);
       }
       private static void WriteComboBoxItems(XmlNode xn)
       {
           XmlNodeList child = xn.ChildNodes;
           for (int i = 0; i < child.Count; i++)
           {

               if (child[i].Name == "Object")
               {
                   WriteComboBoxItems(child[i]);

               }
              
           }

           string type = xn.Attributes["type"].Value;

           if (type != "System.Windows.Forms.ComboBox, System.Windows.Forms"
                  && type != "AnesControlLibrary.DropDownInputBox, AnesControlLibrary")
           {
               return;
           }

           string name = xn.Attributes["name"].Value;

           XmlNode comboxItemsNode = _xmlDocument.CreateElement("Property");
           XmlAttribute nameAttr = _xmlDocument.CreateAttribute("name");
           nameAttr.Value = "ComboBoxItems";
           comboxItemsNode.Attributes.Append(nameAttr);

           ComboBoxElement comboxElement = _comboxElementDictionary[name];
           foreach (ComboxItem item in comboxElement.Items)
           {
               XmlNode itemNode = _xmlDocument.CreateElement("ComboBoxItem");

               XmlAttribute textAttr = _xmlDocument.CreateAttribute("text");
               textAttr.Value = item.DisplayText;

               XmlAttribute valueAttr = _xmlDocument.CreateAttribute("value");
               valueAttr.Value = item.Data;

               itemNode.Attributes.Append(textAttr);
               itemNode.Attributes.Append(valueAttr);

               comboxItemsNode.AppendChild(itemNode);

           }
           XmlNode oldNode = xn.SelectSingleNode("Property[@name='ComboBoxItems']");
           if (oldNode != null)
           {
               xn.ReplaceChild(comboxItemsNode, oldNode);
           }
           else
           {
               xn.AppendChild(comboxItemsNode);
           }
           
       }
       private static void ReadComboBox(XmlNode xn)
       {
          
           XmlNodeList child = xn.ChildNodes;
           for (int i = 0; i < child.Count; i++)
           {

               if (child[i].Name == "Object")
               {
                   ReadComboBox(child[i]);

               }
              
           }
           string type = xn.Attributes["type"].Value;

           if (type != "System.Windows.Forms.ComboBox, System.Windows.Forms"
                  && type != "AnesControlLibrary.DropDownInputBox, AnesControlLibrary")
           {
               return;
           }

           string name = xn.Attributes["name"].Value;
           XmlNode tag = xn.SelectSingleNode("Property[@name='Tag']");

           if (tag == null || string.IsNullOrEmpty(tag.ToString()))
               return;

           string valueMember = string.Empty;
           string displayMember = string.Empty;

           XmlNode valueMemberNode = xn.SelectSingleNode("Property[@name='ValueMember']");
           XmlNode displayMembeNoder = xn.SelectSingleNode("Property[@name='DisplayMember']");

           if (valueMemberNode != null)
               valueMember = valueMemberNode.InnerText;

           if (displayMembeNoder != null)
               displayMember = displayMembeNoder.InnerText;


           XmlDocument tagDocument = new XmlDocument();
           tagDocument.LoadXml(tag.InnerText);

           XmlNode root = tagDocument.ChildNodes[0];

           string dataMember = root.Attributes["name"].Value;
           string rowFilter = string.Empty;
           foreach (XmlNode rowFilterNode in root.ChildNodes)
           {
               if (string.IsNullOrEmpty(rowFilter))
                   rowFilter = rowFilterNode.InnerText;
               else
                   rowFilter += " and " + rowFilterNode.InnerText;
           }

           ComboBoxElement comboxElement = new ComboBoxElement(rowFilter, dataMember, displayMember, valueMember, name);

           _comboxElementDictionary.Add(comboxElement.Name, comboxElement);
       }
    }
}
